package xxy.com.customview;

import android.app.ActivityOptions;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import xxy.com.customview.R;

public class Main2Activity extends AppCompatActivity {

    private Toolbar toolbar;
    private FloatingActionButton fab;
    private ImageView iv;

    public final static int RETURN_OK = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.e("Life","main2_onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        init();
        initView();
        setClickListener();
    }

    public void init() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        fab = (FloatingActionButton) findViewById(R.id.fab);
        iv = (ImageView) findViewById(R.id.main2_iv);
    }

    private void initView() {
        //设置toolbar
        toolbar.setTitle("2018 新年新气象");
        toolbar.setTitleTextColor(getResources().getColor(R.color.text_white));
        toolbar.setSubtitle("变成更好的自己");
        toolbar.setSubtitleTextColor(getResources().getColor(R.color.text_gray));
        toolbar.setLogo(R.mipmap.ic_launcher);
        setSupportActionBar(toolbar);
        //设置导航图标要在setSupportActionBar方法之后
        toolbar.setNavigationIcon(R.drawable.items);

        iv.setBackgroundResource(R.drawable.women);
        AnimationDrawable animationDrawable = (AnimationDrawable) iv.getBackground();
        animationDrawable.start();
    }

    /**
     * toobar菜单的视图准备方法
     * @param menu
     * @return
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private void setClickListener() {
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "想去下一页探秘吗?", Snackbar.LENGTH_LONG)
                        .setAction("好的", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                                    startActivity(new Intent(Main2Activity.this, Main3Activity.class), ActivityOptions.makeSceneTransitionAnimation(Main2Activity.this).toBundle());
                                }
                            }
                        }).show();
            }
        });
        //toolbar的点击事件
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_search:
                        Toast.makeText(Main2Activity.this, "Search !", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_notifications:
                        Toast.makeText(Main2Activity.this, "Notificationa !", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.action_settings:
                        Toast.makeText(Main2Activity.this, "Settings !", Toast.LENGTH_SHORT).show();
                        break;
                }
                return true;
            }
        });

    }

    public void main2OnClick(View view) {
        switch (view.getId()) {
            case R.id.main2_bt:
                onBackPressed();
//                finish();
                break;
        }
    }


    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("Life","main2_onRestart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("Life","main2_onResume");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.e("Life","main2_onRestoreInstanceState");
    }

    @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        Log.e("Life","main2_onSaveInstanceState");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.e("Life","main2_onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.e("Life","main2_onStop");
    }


    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        //设置要返回给上一个activity的东西, 需要注释掉 super.onBackPressed() 否则不成功
        Intent intent = new Intent();
        intent.putExtra("test","I like NBA");
        setResult(RETURN_OK,intent);
        finish();
        overridePendingTransition(R.anim.transiton_in, R.anim.transiton_out);
        Log.e("destroy","onBackPressed");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Life","main2_onDestroy");
        Toast.makeText(this, "destroy", Toast.LENGTH_SHORT).show();
    }
    /**
     * 判断后台线程是否在运行 1、没有   则创造线程    2、有  线程还在巡行，则创建dialog
     * 旋转屏幕 1、销毁dialog   2、保存线程进程
     */
}
